package com.henu.rescue.controller;

import com.henu.rescue.entity.Accident;
import com.henu.rescue.entity.Order;
import com.henu.rescue.entity.Station;
import com.henu.rescue.entity.VO.OrderVO;
import com.henu.rescue.service.OrderService;
import com.henu.rescue.service.StaService;
import com.henu.rescue.utils.Page;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("order/")
@Api("工单管理controller")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private StaService staService;

    @ApiOperation("分页查询")
    @PostMapping("getAllByPage")
    public Result<Page<OrderVO>> getAllByPage(@RequestBody Page<OrderVO> page){
        page.setSortMethod("desc");
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"order_id","station_id","acc_id","status","evaluate_id"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page.setSortMethod("create_time");
        page.setSortMethod("ASC");
        page= orderService.selectOrderInfoByPage(page);
        return new Result(ResultEnums.QUERY_SUCCESS,page);
    }

    @ApiOperation("逻辑删除")
    @DeleteMapping("delOrder/{orderId}")
    public Result delOrder(@PathVariable String orderId){
        orderService.delByLogic(orderId);
        return new Result(ResultEnums.SUCCESS,"事故信息删除成功！");
    }

    @ApiOperation("修改订单状态")
    @PostMapping("updateStatus")
    public Result updateStatus(@RequestBody Map<String, Object> requestBody){
        String orderId = (String) requestBody.get("orderId");
        Integer status = (Integer) requestBody.get("status");
        //判断状态是否合法
        if(!(status>=0 && status<=2)){
            return new Result(ResultEnums.PARAMS_ERROR);
        }
        if(status == 2){
            //将工单结束时间设置为系统时间
            Date currentTime = new Date();
            Order order = orderService.getOrderById(orderId);
            if(order == null){
                return new Result(ResultEnums.UPDATE_FAILED);
            }
            Integer status1 = order.getStatus();
            if(status.compareTo(status1) == 0){
                return new Result(ResultEnums.UPDATE_FAILED,"已经确实完成order");
            }
            order.setEndTime(currentTime);
            orderService.updateOrder(order);
            //获得完成任务的负责救援服务点的id，然后将station_id对应的服务点的处理数量 -1
            Integer stationId = order.getStationId();
            Station station = staService.getStaById(stationId);
            Integer staStatus = station.getStaStatus();
            station.setStaStatus( staStatus - 1);
            staService.update(station);
        }
        //状态合法
        orderService.updateStatus(orderId,status);
        return new Result(ResultEnums.SUCCESS);
    }
}
